<?php
namespace jy_daijia;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class order extends \plugin{
	//订单创建时，执行此方法，执行相关操作
	public function create($params,$uid='',$url=''){
		$this->order=$params;
		$this->order['createtime']=TIMESTAMP;
		$uid=$uid?$uid:$_SESSION['uid'];
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'ordersn'=>$params['ordersn']?:get_ordersn('jy_daijia_order','ordersn'),
			'mid'=>$params['mid']?:$uid,
			'start_seat'=>$params['start_seat'],
			'dest_seat'=>$params['dest_seat'],
			'gocity'=>$params['gocity'],
			'juli'=>$params['juli'],
			'time'=>$params['time'],
			'price1'=>$params['price1'],
			'price2'=>$params['price2'],
			'price'=>$params['price'],
			'waitfee'=>$params['waitfee'],
			'lat'=>$params['lat'],
			'end_lat'=>$params['end_lat'],
			'name'=>$params['name'],
			'mobile'=>$params['mobile'],
		);
		$order['createtime']=TIMESTAMP;
		pdo_insert('jy_daijia_order',$order);
		$orderid=pdo_insertid();
		$params=array(
			'mid'=>$order['mid'],
			'message'=>'您呼叫了一次代驾！',
		);
		$this->model('notice')->add_msg($params);
		return $orderid;
	}
	public function get_order($order_id,$merchid=0){
		if($this->order && $this->order['id']==$order_id){
			return $this->order;
		}
		$this->order=$this->get('order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		return $this->order;
	}
	public function get_fee($juli,$waittime=0,$gocity=''){
		$setting=$this->plugin_setting('setting');
		$fees=array('price'=>0,'price1'=>0,'price2'=>0,'waitfee'=>0,'backfee'=>0);
		//深夜：
		if($setting['ye_time']['start'] && $setting['ye_time']['end']){
				$days=date('Y-m-d',TIMESTAMP);
				$start=strtotime($days.' '.$setting['ye_time']['start']);
				$end=strtotime($days.' '.$setting['ye_time']['end']);
				if($start>$end){
					//0点前
					if(TIMESTAMP>$start && TIMESTAMP>$end){
						$end=$end+24*3600;
					}
					//0点后
					if(TIMESTAMP<$start && TIMESTAMP<$end){
						$start=$start-24*3600;
					}
				}
				if(TIMESTAMP>$start && TIMESTAMP<$end){
					//重置数据
					$setting['start']=$setting['ye_start'];
					$setting['juli']=$setting['ye_juli'];
					$setting['price1']=$setting['ye_price1'];
					$setting['maxjuli']=$setting['ye_maxjuli'];
					$setting['price2']=$setting['ye_price2'];
				}
		}
		if($juli>0){
			if($juli > $setting['maxjuli']){
					$fees['price2']=($juli-$setting['maxjuli'])*$setting['price2'];
					$fees['price1']=($setting['maxjuli']-$setting['juli'])*$setting['price1']+$setting['start'];
			}elseif($juli>$setting['juli'] && $juli <= $setting['maxjuli']){
					$fees['price1']=($juli-$setting['juli'])*$setting['price1']+$setting['start'];
			}else{
					$fees['price1'] = $setting['start'];
			}
		}else{
			$fees['price1'] = $setting['start'];
		}
		//等待费用
		$waittime=intval($waittime/60);
		if($setting['waitfee'] && $setting['fee_waittime'] && $waittime>$setting['fee_waittime']){
			$fees['waitfee']=$setting['waitfee']*($waittime-$setting['fee_waittime']);
		}
		//返城费
		if($setting['backfee_rate'] && $setting['pcd']['city'] && $gocity && $setting['pcd']['city'] != $gocity){
			$fees['backfee']=($fees['price1']+$fees['price2'])*intval($setting['backfee_rate'])/100;
		}
		$fees['price']=$fees['price1']+$fees['price2']+$fees['waitfee']+$fees['backfee'];
		return $fees;
	}
	//订单取消
	public function order_cancel($order_id,$istask=false){
		$this->get_order($order_id);
		if(!$this->order || ($this->order['status'] !=0 && $this->order['status'] !=2 && $this->order['status'] !=6)){
			if($istask){
				return false;
			}else{
				exi('订单不存在或者不支持取消！','error');
			}
		}
		$this->update('order',array('status'=>3),array('id'=>$order_id));
		$params=array(
			'mid'=>$this->order['mid'],
			'message'=>'您的《'.$this->order['start_seat'].'~~'.$this->order['dest_seat'].'》订单已取消！',
		);
		$this->model('notice')->add_msg($params);
		if($this->order['driver_id']){
			pdo_update('jy_daijia_driver',array('busy'=>1),array('id'=>$this->order['driver_id']));
		}
		return true;
	}
	//支付
	public function order_pay($order_id,$params,$url=''){
		$this->get_order($order_id);
		if(!$this->order || $this->order['ispay']){
			exi('订单不存在或者已支付！','error');
		}
		$update=array(
				'paytype'=>$params['paytype'],
				'transid'=>$params['transid'],
		);
		if($params['paytype']=='hdfk'){
			$update['ispay']=0;
		}else{
			$update['ispay']=1;
			$update['pay_time']=TIMESTAMP;
		}
		$params=array(
			'mid'=>$this->order['mid'],
			'message'=>'您的《'.$this->order['start_seat'].'~~'.$this->order['dest_seat'].'》订单已支付！',
		);
		$this->model('notice')->add_msg($params);
		$this->pdo_update('order',$update,array('id'=>$order_id));
		return true;
	}
	public function add_baobei($order_id,$msg='',$oldstatus=2,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'oldstatus'=>$oldstatus?:2,
			'order_id'=>$order_id,
			'mid'=>$mid,
			'msg'=>$msg?:'异常报备',
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('baobei_log',$data);
		$logid=pdo_insertid();
		return $logid;
	}
	public function change_fee($order_id){
		$this->get_order($order_id);
		$setting=$this->plugin_setting('setting');
		if($setting['fee_price']){
			$fee=$setting['fee_price'];
		}elseif($setting['fee_rate']){
			$fee=$setting['fee_rate']*$this->order['price']/100;
		}
		$mid=pdo_getcolumn('jy_daijia_driver',array('id'=>$this->order['driver_id']),'mid');
		model('core/member')->cut_credit2($mid,$fee,'司机扣手续费','jy_daijia');
		return true;
	}
	public function cel_upfee($order){
		$setting=$this->plugin_setting('setting');
		$setting['cel_time']=$setting['cel_time']?:3;
		$setting['cel_fee']=$setting['cel_fee']?:3;
		$money=$order['price']*$setting['cel_fee']/100;
		if($order['createtime'] <=time()-$setting['cel_time']*60){
			return $money;
		}
		return 0;
	}
	public function cel_check($order,$res=false){
		$setting=$this->plugin_setting('setting');
		$setting['cel_time']=$setting['cel_time']?:3;
		$setting['cel_fee']=$setting['cel_fee']?:3;
		$money=$order['price']*$setting['cel_fee']/100;
		if($order['createtime'] <=time()-$setting['cel_time']*60){
			if($res){
				return $setting['cel_time'].'分钟后取消订单，目前需要付费'.$money.'元';
			}else{
				exi($setting['cel_time'].'分钟后取消订单，目前需要付费'.$money.'元','error');
			}
		}
		return false;
	}
}